package com.hycat.mail.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hycat.mail.common.constant.ProductConstant;
import com.hycat.mail.common.exception.MailException;
import com.hycat.mail.common.model.ExceptionEnum;
import com.hycat.mail.product.service.BrandService;
import com.hycat.mail.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hycat.mail.common.utils.PageUtil;
import com.hycat.mail.common.utils.Query;

import com.hycat.mail.product.dao.CategoryBrandRelationDao;
import com.hycat.mail.product.entity.CategoryBrandRelationEntity;
import com.hycat.mail.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtil(page);
    }

    @Override
    public void saveDetails(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        String brandName = brandService.getById(brandId).getName();
        String catelogName = categoryService.getById(catelogId).getName();
        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelation.setCatelogName(catelogName);
        save(categoryBrandRelation);
    }

    @Override
    public void updateDetails(Long id, String name ,String sign) {
        UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper<CategoryBrandRelationEntity>();
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        if(sign.equals(ProductConstant.BRAND)) {
            categoryBrandRelationEntity.setBrandId(id);
            categoryBrandRelationEntity.setBrandName(name);
            wrapper.eq("brand_id", id);
        } else if(sign.equals(ProductConstant.CATEGORY)) {
            categoryBrandRelationEntity.setCatelogId(id);
            categoryBrandRelationEntity.setCatelogName(name);
            wrapper.eq("catelog_id", id);
        } else {
             throw new MailException(ExceptionEnum.CATEGORY_BRAND_RELATION_PARAM_UPDATE_EXCEPTION);
        }
        update(wrapper);
    }

}